<script>
import FormRender from '@/components/FreeForm/render/index.vue'
import {getBpmRule} from '@/api/bpm/rules'
import {isEmpty} from 'lodash'
import {getActiveProcessDefinitionByKey} from '@/api/bpm/definition'
import ProcessImage from "@/components/PrcoessImage/index.vue";

export default {
  name: 'ReadonlyTask',
  components: {ProcessImage, FormRender},
  data() {
    return {
      ctx: {
        data: null,
        vars: null,
        formKey: null,
        businessKey: null
      },
      visible: false
    }
  },
  created() {
    this.taskInfo = this.$route.query
    this.ctx.businessKey = this.taskInfo.businessKey
    this.getProcessRule()
    this.getProcessDefinitionInfo()
  },
  methods: {
    getProcessRule() {
      const data = {
        processInstanceId: this.taskInfo.processInstanceId,
        modelKey: this.taskInfo.processDefinitionKey,
        nodeCode: this.taskInfo.taskDefinitionKey
      }
      getBpmRule(data).then(res => {
        this.bpmRule = res.data
        if ((isEmpty(this.bpmRule) || isEmpty(this.bpmRule.formCode))) {
          return this.$message.error('流程未配置页面，请配置对应的处理页面')
        }
        this.ctx.formKey = this.bpmRule.formCode
        this.taskInfo.name = this.taskInfo.name || this.bpmRule.nodeName
        this.taskInfo.taskDefinitionKey = this.taskInfo.taskDefinitionKey || this.bpmRule.nodeCode
        this.taskInfo.comment = this.bpmRule.comment || ''
        if (this.bpmRule.defaultBtnEvent && this.bpmRule.defaultBtnEvent.length > 0) {
          this.handlers.executeCmd = this.bpmRule.defaultBtnEvent
        } else if (this.bpmRule.btnEvents && this.bpmRule.btnEvents.length > 0) {
          this.handlers.executeCmd = this.bpmRule.btnEvents[0]
        }
        this.handlers.comment = this.taskInfo.comment
        this.$forceUpdate()
      })
    },
    getProcessDefinitionInfo() {
      if (!this.taskInfo.processDefinitionKey) return
      getActiveProcessDefinitionByKey(this.taskInfo.processDefinitionKey).then(res => {
        this.definition = res.data
        this.taskInfo.processDefinitionKey = this.definition.id
      })
    }
  }
}
</script>

<template>
  <el-container class="handle-task">
    <el-main>
      <form-render
        :form-code="ctx.formKey"
        :business-key="ctx.businessKey"
        :is-design="false"
        :readonly="true"
      />
    </el-main>
    <template>
      <el-dialog title="查看流程图" :visible.sync="visible" width="70%">
        <process-image :process-definition-id="taskInfo.processDefinitionKey" :process-instance-id="taskInfo.processInstanceId"/>
      </el-dialog>
    </template>
    <el-footer>
      <div class="handle-task__footer">
        <el-button size="small" icon="el-icon-back" @click="$router.back()">返回</el-button>
        <el-button size="small" icon="el-icon-s-home" @click="$router.push('/')">首页</el-button>
        <el-button size="small" icon="el-icon-picture" @click="visible = true">流程图</el-button>
      </div>
    </el-footer>
  </el-container>
</template>

<style scoped lang="scss">
.handle-task {
  position: absolute;
  height: 100%;
  width: 100%;
  background: #f3f3f3;

  .handle-task__footer {
    height: 100%;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    border-top: 1px solid #dcdcdc;
  }

  .el-footer {
    padding: 0;
    background: #fff;
  }

  ::v-deep .el-form-item {
    margin-bottom: 10px;
  }
}
</style>
